var xml = new XMLHttpRequest();
var rendererOptions = {
	draggable : true
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
var directionsService = new google.maps.DirectionsService();
var geocoder;
var map;
var stepDisplay;
var markerArray = [];
var geocoder = new google.maps.Geocoder();

function escreveMapa() {
	var num = document.getElementById("idTrajeto").value;
	xml.onreadystatechange = function(){
		if (xml.readyState == 4) {			
			var f = document.getElementsByTagName("fieldset")[0];
			var x = document.createElement("div");
			var v = xml.responseText;
			var lista = [];
			lista = v.split(",");
			var waypts = [];
			var start = null;
			var end = null;

			for ( var i = 0; i < lista.length; i++) {
				x.innerHTML += lista[i];
				f.appendChild(x);
			}

			for ( var i = 0; i < lista.length; i += 2) {
				
				var lat = parseFloat(lista[i]);
				var lng = parseFloat(lista[i + 1]);
				var laa = lat+","+lng;		
				
				waypts.push(laa);
				
				if (i == 0) {
					start = laa;
				}
				if (i == lista.length - 2) {
					end = laa;
				}								
			}

			//calcRoute(start, end);
			calcRouteWithWaypoints(start, end, waypts);
		}	
	};
	//xml.open("post", "CadastrarCarona"); GetCarona
	xml.open("post", "GetTrajeto"); 
	xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xml.send("num=" + encodeURIComponent(num));
}

function initialize() {
	initializeMap();
	escreveMapa();
}

onload = initialize;
